package com.example.administrator.newxlmedical;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseActivity;

import java.math.BigDecimal;

import beans.NewUserInfoBean;
import butterknife.BindView;
import butterknife.OnClick;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.ActivityUtil;
import utils.FormateUtils;
import utils.ObjectSaveUtil;

public class OpenDoorActivity extends BaseActivity {

    @BindView(R.id.tv_select_cabient_first)
    TextView tvSelectCabientFirst;
    @BindView(R.id.tv_select_cabient_second)
    TextView tvSelectCabientSecond;
    @BindView(R.id.tv_select_cabient_third)
    TextView tvSelectCabientThird;
    @BindView(R.id.tv_select_cabient_fourth)
    TextView tvSelectCabientFourth;
    @BindView(R.id.tv_select_cabient_fifth)
    TextView tvSelectCabientFifth;
    @BindView(R.id.tv_open_door_activity_open)
    TextView tvOpen;
    @BindView(R.id.iv_select_cabient_first)
    ImageView ivSelectCabientFirst;
    @BindView(R.id.iv_select_cabient_second)
    ImageView ivSelectCabientSecond;
    @BindView(R.id.iv_select_cabient_third)
    ImageView ivSelectCabientThird;
    @BindView(R.id.iv_select_cabient_fourth)
    ImageView ivSelectCabientFourth;
    @BindView(R.id.iv_select_cabient_fifth)
    ImageView ivSelectCabientFifth;
    @BindView(R.id.iv_common_toolbar_home)
    ImageView ivCommonToolbarHome;
    //用于判断选中的是哪个柜子
    private int selectCabinet = 0;
    //用于判断上一次选中的是哪个柜子
    private int beforeSelect = 0;
    private MyHandler allHandler;
    //用于判断MainActivity的点击行为,取出,盘点,验收,归还
    private int type;

    //布尔值,用于判断是否可点击
    private boolean isClickOne, isClickSecond, isClickThird, isClickFour, isClickFive, isClickOpen,isInventory;

    private SharedPreferences sharedPreferences;

    private NewUserInfoBean.DataBean userInfoBean;
    private MediaPlayer mediaPlayer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_door);
    }

    @Override
    public void initView() {
        sharedPreferences = ActivityUtil.getSharedPreferences(OpenDoorActivity.this);

        userInfoBean = (NewUserInfoBean.DataBean) ObjectSaveUtil.readObject(OpenDoorActivity.this, Constants.USER_INFO_BEAN, Constants.USER_INFO_BEAN);
        type = getIntent().getIntExtra(Constants.OPEN_DOOR, 0);
        if (type == Constants.OPEN) {
            mediaPlayer = MediaPlayer.create(OpenDoorActivity.this, R.raw.check_cabinet_open);
            tvOpen.setText("开门");
        } else if (type == Constants.CHECK) {
            mediaPlayer = MediaPlayer.create(OpenDoorActivity.this, R.raw.check_cabinet_check);
            tvOpen.setText("验收");
        } else {
            mediaPlayer = MediaPlayer.create(OpenDoorActivity.this, R.raw.check_cabinet_inventory);
            tvOpen.setText("盘点");
        }
        int sound = sharedPreferences.getInt(Constants.SOUND_KEY, 50);
        Float div = FormateUtils.div(sound, 100, 1);
        mediaPlayer.setVolume(div,div);
        mediaPlayer.start();

        allHandler = SingletonClass.getSingleton(MyHandler.class);
        //轮询
        Message message = allHandler.obtainMessage();
        message.obj = userInfoBean.getRoleInfoId();
        message.what = HandlerConstant.ROLL;
        allHandler.sendMessage(message);
    }

    public void selectCabinetClick(int i) {
        selectCabinet = i;
        if (selectCabinet != beforeSelect) {
            if (beforeSelect == 1) {
                ivSelectCabientFirst.setVisibility(View.GONE);
            } else if (beforeSelect == 2) {
                ivSelectCabientSecond.setVisibility(View.GONE);
            } else if (beforeSelect == 3) {
                ivSelectCabientThird.setVisibility(View.GONE);
            } else if (beforeSelect == 4) {
                ivSelectCabientFourth.setVisibility(View.GONE);
            } else if (beforeSelect == 5) {
                ivSelectCabientFifth.setVisibility(View.GONE);
            }

            tvOpen.setBackground(getResources().getDrawable(R.drawable.btn_blue_shape));

            Message message = allHandler.obtainMessage();
            message.obj = selectCabinet;
            message.what = HandlerConstant.FLASH_ON;
            allHandler.sendMessage(message);

            if (beforeSelect != 0) {
                Message message1 = allHandler.obtainMessage();
                message1.obj = beforeSelect;
                message1.what = HandlerConstant.FLASH_OFF;
                allHandler.sendMessageDelayed(message1, 50);
            }
            beforeSelect = selectCabinet;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mediaPlayer.stop();

    }

    @Override
    protected void onHandleMessage(Message msg) {
        switch (msg.what) {

            case HandlerConstant.ROLL_SUCCESS:
                initSharedPreferences((String) msg.obj);
                break;

            case HandlerConstant.NO_OPERATION:
                Message message = allHandler.obtainMessage();
                message.obj = selectCabinet;
                message.what = HandlerConstant.FLASH_OFF;
                allHandler.sendMessage(message);
                break;

            case HandlerConstant.OPEN_DOOR:
                Log.e(TAG, "onHandleMessage: OPEN_DOOR" );
                    Intent intent = new Intent();
                    if (type == Constants.OPEN) {
                        Log.e(TAG, "onHandleMessage: OPEN" );
                        intent.setClass(OpenDoorActivity.this, OpenDoorRemindActivity.class);
                        intent.putExtra(Constants.OPEN_DOOR_REMIND, type);
                        intent.putExtra(Constants.OPEN_DOOR_CABINET, selectCabinet);
                    } else if (type == Constants.CHECK) {
                        Log.e(TAG, "onHandleMessage: CHECK" );
                        intent.setClass(OpenDoorActivity.this, OpenDoorRemindActivity.class);
                        intent.putExtra(Constants.OPEN_DOOR_REMIND, type);
                        intent.putExtra(Constants.OPEN_DOOR_CABINET, selectCabinet);
                    }
                    startActivity(intent);
                    finish();
                break;

        }
    }

    //初始化柜子的连接状态
    private void initSharedPreferences(String address) {
        Drawable topDrawable = getResources().getDrawable(R.mipmap.door_connect);
        topDrawable.setBounds(0, 0, topDrawable.getMinimumWidth(), topDrawable.getMinimumHeight());
        if (sharedPreferences != null) {
            if (sharedPreferences.getString(Constants.ADDRESS_FIRST, "").equals(address)) {
                tvSelectCabientFirst.setCompoundDrawables(null, topDrawable, null, null);
                isClickOne = true;
            } else if (sharedPreferences.getString(Constants.ADDRESS_SECOND, "").equals(address)) {
                tvSelectCabientSecond.setCompoundDrawables(null, topDrawable, null, null);
                isClickSecond = true;
            } else if (sharedPreferences.getString(Constants.ADDRESS_THIRD, "").equals(address)) {
                tvSelectCabientThird.setCompoundDrawables(null, topDrawable, null, null);
                isClickThird = true;
            } else if (sharedPreferences.getString(Constants.ADDRESS_FOURTH, "").equals(address)) {
                tvSelectCabientFourth.setCompoundDrawables(null, topDrawable, null, null);
                isClickFour = true;
            } else if (sharedPreferences.getString(Constants.ADDRESS_FIFTH, "").equals(address)) {
                tvSelectCabientFifth.setCompoundDrawables(null, topDrawable, null, null);
                isClickFive = true;
            }
        }
    }

    @OnClick({R.id.tv_select_cabient_first, R.id.tv_select_cabient_second, R.id.tv_select_cabient_third, R.id.tv_select_cabient_fourth, R.id.tv_select_cabient_fifth, R.id.tv_open_door_activity_open,R.id.iv_common_toolbar_home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_select_cabient_first:
                if (isClickOne) {
                    ivSelectCabientFirst.setVisibility(View.VISIBLE);
                    selectCabinetClick(1);
                }
                break;
            case R.id.tv_select_cabient_second:
                if (isClickSecond) {
                    ivSelectCabientSecond.setVisibility(View.VISIBLE);
                    selectCabinetClick(2);
                }
                break;
            case R.id.tv_select_cabient_third:
                if (isClickThird) {
                    ivSelectCabientThird.setVisibility(View.VISIBLE);
                    selectCabinetClick(3);
                }
                break;
            case R.id.tv_select_cabient_fourth:
                if (isClickFour) {
                    ivSelectCabientFourth.setVisibility(View.VISIBLE);
                    selectCabinetClick(4);
                }
                break;
            case R.id.tv_select_cabient_fifth:
                if (isClickFive) {
                    ivSelectCabientFifth.setVisibility(View.VISIBLE);
                    selectCabinetClick(5);
                }
                break;
            case R.id.tv_open_door_activity_open:
                if (selectCabinet != 0 && selectCabinet < 6 && type != 0) {
                    isClickOpen = true;
                    Message message = allHandler.obtainMessage();
                    message.obj = selectCabinet;
                    if (type == Constants.OPEN) {
                        message.what = HandlerConstant.TAKE_OUT;
                    } else if (type == Constants.CHECK) {
                        message.what = HandlerConstant.CHECK;
                    }
                    allHandler.sendMessage(message);
                }
                break;
            case R.id.iv_common_toolbar_home:
                Message message = allHandler.obtainMessage();
                message.obj = selectCabinet;
                message.what = HandlerConstant.FLASH_OFF;
                allHandler.sendMessage(message);
                finish();
                break;
        }
    }

}
